#include "card.h"

Card::Card() {}

Card::Card(CardPoint point, CardSuit suit)
{
    setPoint(point);
    setSuit(suit);
}

// 设置花色
void Card::setSuit(CardSuit suit)
{
    m_suit = suit;
}


// 设置点数
void Card::setPoint(CardPoint point)
{
    m_point = point;
}

// 获取花色
Card::CardSuit Card::getSuit() const
{
    return m_suit;
}


// 获取点数
Card::CardPoint Card::getPoint() const
{
    return m_point;
}


// 对象的比较
bool lessSort(const Card &c1, const Card &c2)
{
    if (c1.getPoint() == c2.getPoint()) {
        return c1.getSuit() < c2.getSuit();
    } else {
        return c1.getPoint() < c2.getPoint();
    }
}

// 对象的比较
bool greaterSort(const Card &c1, const Card &c2)
{
    if (c1.getPoint() == c2.getPoint()) {
        return c1.getSuit() > c2.getSuit();
    } else {
        return c1.getPoint() > c2.getPoint();
    }
}

bool operator <(const Card& c1, const Card& c2) {
    return lessSort(c1, c2);
}

// 操作符重载（==）
bool operator ==(const Card& left, const Card& right) {
    return left.getPoint() == right.getPoint() && left.getSuit() == right.getSuit();
}

// 添加友元函数
QDataStream& operator <<(QDataStream& in, const Card& card)
{
    int suit = static_cast<int>(card.getSuit());
    int point = static_cast<int>(card.getPoint());
    in << suit << point;
    return in;
}

QDataStream& operator >>(QDataStream& out, Card& card)
{
    int suit, point;
    out >> suit >> point;
    card.setSuit(static_cast<Card::CardSuit>(suit));
    card.setPoint(static_cast<Card::CardPoint>(point));

    return out;
}

// 重写全局函数 qHash （获取不同扑克牌的哈希值）
uint qHash(const Card &card)
{
    return card.getPoint() * 100 + card.getSuit();
}
